package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.project.entity.Grade;
import com.project.entity.Role;
import com.project.entity.User;
import com.project.service.GradeService;
import com.project.service.RoleService;
import com.project.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;

import java.lang.reflect.Array;
import java.util.*;

@RestController
@RequestMapping("grade")
public class GradeController {
    @Autowired
    private GradeService gradeService;
    @Autowired
    private RoleService roleService;
    /**
     * 用户服务
     */
    @Autowired
    private UserService userService;

    /**
     * 动态条件查询用户
     *
     * @param pageNO 页码
     * @param username    用户名
     * @return {@code IPage<User>}
     */
    @RequestMapping("selectUserByName")
    public IPage<User> selectUserByName(Integer pageNO,String username){

        return userService.selectUserByName(pageNO, username);
    }

    /**
     * 添加用户
     *
     * @param username 用户名
     * @param trueName 真正名字
     * @param role     角色集合
     * @return {@code String}
     */
    @RequestMapping("addUser")
    public String addUser(String username,
                          String trueName,
                      @RequestParam  ArrayList<Integer> role){
        System.out.println("role:"+role+" "+username+" "+trueName);
        if (userService.addUser(username, trueName, role).equals("ok")){
            return "ok";
        }
        else if (userService.addUser(username,trueName,role).equals("该用户已存在")){
            return "该用户已存在";
        }
        else {
            return "添加失败";
        }
//        return "role:"+role+" "+username+" "+trueName;
    }

    /**
     * 删除用户
     *
     * @param userId 用户id
     * @return {@code String}
     */
    @RequestMapping("deleteUser")
    public String deleteUser(Integer userId){
        userService.deleteUser(userId);
        return "ok";
    }


    /**
     * 查询用户
     *
     * @param userId 用户id
     * @return {@code User}
     */
    @RequestMapping("selectUser")
    public User selectUser(Integer userId){
        return userService.selectUser(userId);
    }

    /**
     * 更新用户角色
     *
     * @param userId     用户id
     * @param newRoleIds 新角色id集合
     * @return {@code String}
     */
    @RequestMapping("updateUserRole")
    public String updateUserRole(Integer userId,@RequestParam ArrayList<Integer> newRoleIds){
        userService.updateUserRole(userId,newRoleIds);
        return "ok";
    }

    /**
     * 分页查询所有角色
     *
     * @param pageNO 页码
     * @return {@code IPage<Role>}
     */
    @RequestMapping("selectRoles")
    public IPage<Role> selectRoles(Integer pageNO){
        return roleService.selectRoles(pageNO);
    }
    /**
     * 查询所有权限
     *
     * @return {@code List<Grade>}
     */
    @RequestMapping("selectAllGrade")
    public List<Grade> selectAllGrade(){
        return roleService.selectAllGrade();
    }
    /**
     * 添加角色
     *
     * @param roleName  角色名
     * @param gradeList 权限集合
     * @return {@code String}
     */
    @RequestMapping("/addRole")
    public String addRole(String roleName, @RequestParam ArrayList<Integer> gradeList){
        if (roleService.addRole(roleName,gradeList).equals("该角色已存在")){
            return "该角色已存在";
        }
        else {
//            System.err.println(gradeList);
            return "ok";
        }
    }

    /**
     * 删除角色
     *
     * @param roleId 角色id
     * @return {@code String} "ok"
     */
    @RequestMapping("deleteRole")
    public String deleteRole(Integer roleId){
        roleService.deleteRole(roleId);
        return "ok";
    }

    /**
     * 查询角色
     *
     * @param roleId 角色id
     * @return {@code Role} 角色对象
     */
    @RequestMapping("selectRole")
    public Role selectRole(Integer roleId) {
        return roleService.selectRole(roleId);
    }

    /**
     * 修改角色权限
     *
     * @param roleId    角色id
     * @param newGrades 新权限集合
     * @return {@code String}
     */
    @RequestMapping("updateRole")
    public String updateRole(Integer roleId,@RequestParam ArrayList<Integer> newGrades){
        roleService.updateRole(roleId,newGrades);
        return "ok";
    }

}
